package com.jsy.common.util.publicApi.aksk;

import sun.misc.BASE64Encoder;

import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

/**
 * gates.huang
 * 2025/07/15
 * AK/SK服务端生成工具
 */
public class AkSkGenerator {
    private static final SecureRandom secureRandom = new SecureRandom();
    private static final BASE64Encoder base64Encoder = new BASE64Encoder();

    /**
     * 生成新的AK/SK对
     */
    public static Map<String, String> generateKeyPair() {
        Map<String, String> keyPair = new HashMap<>();
        
        // 生成Access Key (AK)
        byte[] akBytes = new byte[16]; // 128位
        secureRandom.nextBytes(akBytes);
        String accessKey = "AK-" + base64Encoder.encode(akBytes);
        
        // 生成Secret Key (SK)
        byte[] skBytes = new byte[32]; // 256位
        secureRandom.nextBytes(skBytes);
        String secretKey = base64Encoder.encode(skBytes);
        
        keyPair.put("accessKey", accessKey);
        keyPair.put("secretKey", secretKey);
        
        return keyPair;
    }

    public static void main(String[] args) {
        // 生成并打印AK/SK对
        Map<String, String> keyPair = generateKeyPair();
        System.out.println("Generated Access Key: " + keyPair.get("accessKey"));
        System.out.println("Generated Secret Key: " + keyPair.get("secretKey"));

    }
}